package com.quzhi.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.Result;
import com.quzhi.common.code.Code;
import com.quzhi.sys.dto.OrderDto;
import com.quzhi.sys.entity.GoodsSerialNumber;
import com.quzhi.sys.entity.Order;
import com.quzhi.sys.entity.OrderLog;
import com.quzhi.sys.entity.OrderSerialNumber;
import com.quzhi.sys.mapper.OrderSerialNumberMapper;
import com.quzhi.sys.service.IOrderSerialNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xibing
 * @since 2024-07-01
 */
@Service
public class OrderSerialNumberServiceImpl extends ServiceImpl<OrderSerialNumberMapper, OrderSerialNumber> implements IOrderSerialNumberService {


    @Autowired
    private OrderLogServiceImpl orderLogService;

    @Override
    public String addSerialNumber(String serialNumber, String id, Boolean type, Boolean status) {
        // 查询数据库是否有待出库的序列号和这个序列号一样的
        LambdaQueryWrapper<OrderSerialNumber> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderSerialNumber::getSerialNumber, serialNumber);
        wrapper.eq(OrderSerialNumber::getStatus, Code.DAI_CHU_KU);
        OrderSerialNumber one = this.baseMapper.selectOne(wrapper);

        if (one != null) return "该序列号已绑定订单";

        OrderSerialNumber orderSerialNumber = new OrderSerialNumber();
        // 设置ID
        orderSerialNumber.setId(UUID.randomUUID().toString());

        orderSerialNumber.setOrderId(id);

        orderSerialNumber.setSerialNumber(serialNumber);


        // 设置默认状态(待出库)
        orderSerialNumber.setStatus(status);

        // 设置创建时间
        orderSerialNumber.setCreateTime(LocalDateTime.now());
        // 设置更新时间
        orderSerialNumber.setUpdateTime(LocalDateTime.now());

        this.baseMapper.insert(orderSerialNumber);

        log.error("新增序列号成功-" + orderSerialNumber.getId());
        return "新增序列号成功";
    }


    /**
     * 将序列号绑定订单
     *
     * @param orders
     * @return
     */
    @Override
    public List<OrderDto> getOrderDtoList(List<Order> orders) {

        List<OrderDto> orderDtos = new ArrayList<>();


        for (Order order : orders) {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(order, orderDto);


            // 获取序列号
            LambdaQueryWrapper<OrderSerialNumber> orderSerialNumberLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderSerialNumberLambdaQueryWrapper.eq(OrderSerialNumber::getOrderId, order.getId());
            List<OrderSerialNumber> serialNumberList = this.baseMapper.selectList(orderSerialNumberLambdaQueryWrapper);

            orderDto.setSerialNumbers(serialNumberList);

            // 获取日志
            LambdaQueryWrapper<OrderLog> orderLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderLogLambdaQueryWrapper.eq(OrderLog::getOrderId, order.getId());
            // 排序
            orderLogLambdaQueryWrapper.orderByDesc(OrderLog::getCreateTime);
            List<OrderLog> orderLogs = orderLogService.list(orderLogLambdaQueryWrapper);

            orderDto.setOrderLogs(orderLogs);

            orderDtos.add(orderDto);
        }


        return orderDtos;
    }

    /**
     * 判断序列号是否和其他订单序列号重复
     */
    @Override
    public void addExitLogs(OrderSerialNumber orderSerialNumber) {

    }


    /**
     * 删除重复序列号日志
     */
    @Override
    public void deleteExitLogs(OrderSerialNumber serialNumber) {
        // 查询该订单是否有重复的序列号
        // 查询序列号
        OrderSerialNumber orderSerialNumber = this.baseMapper.selectById(serialNumber);


        if (orderSerialNumber != null) {
            // 所有待出库的序列号
            LambdaQueryWrapper<OrderSerialNumber> orderSerialNumberLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderSerialNumberLambdaQueryWrapper.eq(OrderSerialNumber::getStatus, Code.DAI_CHU_KU);
            List<OrderSerialNumber> list = this.baseMapper.selectList(orderSerialNumberLambdaQueryWrapper);

            // 查询订单绑定的所有序列号
            LambdaQueryWrapper<OrderSerialNumber> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderSerialNumber::getOrderId, orderSerialNumber.getOrderId());
            List<OrderSerialNumber> serialNumberList = this.baseMapper.selectList(wrapper);

            // 查询是否有序列号
            boolean flag = true;
            for (OrderSerialNumber number : serialNumberList) {

                for (OrderSerialNumber sn : list) {
                    // 不查询自己
                    if (number.getSerialNumber().equals(sn.getSerialNumber()) && !number.getId().equals(sn.getId())) {
                        flag = false; // 有序列号重复不删除日志
                        break;
                    }
                }
                if (!flag) break;
            }

            if (flag) {


                // 查询订单所有日志
                LambdaQueryWrapper<OrderLog> orderLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
                orderLogLambdaQueryWrapper.eq(OrderLog::getOrderId, orderSerialNumber.getOrderId());
                List<OrderLog> logs = orderLogService.list(orderLogLambdaQueryWrapper);

                // 删除序列号重复日志
                for (OrderLog log : logs) {
                    if (log.getLog().contains("序列号重复")) {
                        // 删除该日志
                        orderLogService.removeById(log);
                    }
                }
            }

        }

    }


}
